using System;
using System.Collections.Generic;

namespace AreaGenerator
{
	public class RoomTile
	{
		public Node nw;
		public Node ne;
		public Node sw;
		public Node se;
		
		private Room room;
		
		private bool entrance = false;
		private bool exit = false;
		
		public int keyID = 0;
		
		public RoomTile(Node sw){
			this.sw = sw;
			this.nw = sw.north;
			this.se = sw.east;
			this.ne = sw.east.north; // /!\npe
		}
		
		public void setRoom(Room room){
			this.room = room;	
		}
		
		public Room getRoom(){
			return room;	
		}
		
		public bool containsNode(Node n){
			return nw.Equals(n) || sw.Equals(n) || ne.Equals(n) || se.Equals(n);
		}
		
		public bool containsWall(Wall wall){
			return containsNode(wall.n1) && containsNode(wall.n2);
		}
		
		public bool isLeftTile(Wall w){
			bool res = true;
			if (!containsWall(w)){
				UnityEngine.Debug.Log("RoomTile we have a problem !");	
			} else {
				Orientation ori = w.getOrientation();
				Node n1w = null;
				Node n2w = null;
				switch (ori){
					case Orientation.east :
						n1w = w.n1.north;
						n2w = w.n2.north;
						break;
					case Orientation.south :
						n1w = w.n1.east;
						n2w = w.n2.east;
						break;
					case Orientation.west :
						n1w = w.n1.south;
						n2w = w.n2.south;
						break;
					case Orientation.north :
						n1w = w.n1.west;
						n2w = w.n2.west;
						break;
				}
				res = res && n1w != null && n2w != null;
				if (res) res = res && containsNode(n1w) && containsNode(n2w);
			}
			return res;
			
		}
		
		public override bool Equals(Object tile){
			RoomTile t = tile as RoomTile;
			return this.sw.Equals(t.sw);
		}
		
		public override int GetHashCode(){
			return this.sw.GetHashCode();	
		}
		
		public override String ToString(){
			return "Tile : (sw = " + sw + ")";
		}
		
		public void setEntrance(bool b){
			entrance = b;
		}
		
		public void setExit(bool b){
			exit = b;
		}
		
		public bool isEntrance(){
			return entrance;
		}
		
		public bool isExit(){
			return exit;
		}
		
		public List<Wall> get4Walls(){
			List<Wall> res = new List<Wall>();
			res.Add(new Wall(sw, nw));
			res.Add(new Wall(sw, se));
			res.Add(new Wall(nw, ne));
			res.Add(new Wall(se, ne));
			
			
			return res;
		}
		
		
	}
}

